package com.baylor.services;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import com.baylor.common.ConstantValue;
import com.xutils.NettyUtils;
import com.xutils.netty.push.PushCallBack;

import java.io.FileDescriptor;
import java.io.PrintWriter;

/**
 * Created by ketian on 2014/9/24.
 */
public class PushMsgService extends Service {

    private NettyUtils mNettyUtils;

    @Override
    public void onCreate() {
        mNettyUtils = NettyUtils.getInstance();
        try {
            mNettyUtils.startPushService(ConstantValue.IP, ConstantValue.PORT, new PushCallBack(){
                @Override
                public void onConnected() {
                    Log.d("push", "onConnected");
                }

                @Override
                public void onReceiveMsg(Object msg) {
                    Log.d("push", "onReceiveMsg---->"+msg);
                }

                @Override
                public void onDisconnect() {
                    Log.d("push", "连接断开");
                }
            });
        } catch (Exception e) {
            // 在这里，出现异常，关闭连接，重新开启
            Log.d("push", "出现异常，关闭连接");
            mNettyUtils.stopPushService();
            e.printStackTrace();
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}
